package cn.mumzone.pos.util.returngoods;

import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;

import java.util.List;

import cn.mumzone.basic.dialog.AlertDialog;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.basic.util.ToastUtil;
import cn.mumzone.pos.R;
import cn.mumzone.pos.dialog.PosDialog;
import cn.mumzone.pos.model.ReturnGoodsEntity;
import cn.mumzone.pos.ui.activity.ACT_BillReturn;
import cn.mumzone.pos.ui.adapter.ADA_DialogChooseReturnGoods;
import cn.mumzone.pos.ui.widgets.StepView;
import cn.mumzone.pos.ui.widgets.others.StoreHouseHeader;
import cn.mumzone.pos.ui.widgets.refresh.api.RefreshLayout;
import cn.mumzone.pos.ui.widgets.util.StringSpanUtils;

/**
 * Created by JoeLjt on 2019/7/13.
 * Email: lijiateng1219@gmail.com
 * Description:
 */

public class ReturnUtil {

    public interface OnSelectReturnGoodsListener {
        void OnSelectReturnGoodsList(List<ReturnGoodsEntity> list);
    }

    public static void initRecyclerView(Context context, LRecyclerView recyclerView, RecyclerView.Adapter adapter, RefreshLayout refreshLayout) {
        final LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(new LRecyclerViewAdapter(adapter));
        recyclerView.setHasFixedSize(true);
        recyclerView.setPullRefreshEnabled(false);
        recyclerView.setLoadMoreEnabled(false);
        if (refreshLayout != null) {
            refreshLayout.setHeaderHeight(80);
        }
    }

    public static View initReturnDetailToggle(View rootView) {
        LinearLayout llToggleReturn = rootView.findViewById(R.id.ll_toggle_return_amount);
        final LinearLayout llReturnDetail = rootView.findViewById(R.id.ll_return_detail);
        final TextView tvToggle = rootView.findViewById(R.id.tv_toggle);
        final AppCompatImageView ivToggle = rootView.findViewById(R.id.iv_toggle);
        llToggleReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isDetailVisible = llReturnDetail.getVisibility() == View.VISIBLE;
                if (isDetailVisible) {
                    tvToggle.setText("展开明细");
                    ivToggle.setImageResource(R.drawable.svg_ic_toggle_show_return_detail);
                    llReturnDetail.setVisibility(View.GONE);
                } else {
                    tvToggle.setText("收起明细");
                    ivToggle.setImageResource(R.drawable.svg_ic_toggle_hide_return_detail);
                    llReturnDetail.setVisibility(View.VISIBLE);
                }
            }
        });
        return llToggleReturn;
    }

    public static View switchReturnStep(Context context, StepView stepView, int targetLayoutId, FrameLayout container, SparseArray<View> stepArr) {
        final View stepLayout = View.inflate(context, targetLayoutId, null);
        container.removeAllViews();

        if (targetLayoutId == R.layout.layout_bill_return_step_one) {
            stepView.setCurrentPosition(0);
            TextView tvEmptyHint = stepLayout.findViewById(R.id.tv_empty_hint);
            tvEmptyHint.setText(StringSpanUtils.init().addStr("您可以直接扫码或者搜索选择您退货的商品哦~")
                    .setForeColorWithKeyAndPosition("扫码", 0, context.getResources().getColor(R.color.order_status_service))
                    .setForeColorWithKeyAndPosition("搜索", 0, context.getResources().getColor(R.color.order_status_service))
                    .build());
        } else if (targetLayoutId == R.layout.layout_bill_return_step_two) {
            stepView.setCurrentPosition(1);
        }

        container.addView(stepLayout);
        stepArr.put(targetLayoutId, stepLayout);
        return stepLayout;
    }

    public static void setLeftLayoutVisible(View rootView, View leftView, boolean visible) {
        leftView.setVisibility(visible ? View.VISIBLE : View.GONE);
        ViewGroup.LayoutParams params = rootView.getLayoutParams();
        params.width = visible ? 1040 : 520;
        rootView.setLayoutParams(params);
    }

    public static void showSelectGoodsDialog(final Context context, final List<ReturnGoodsEntity> localList, final List<ReturnGoodsEntity> newList, final OnSelectReturnGoodsListener listener) {

        final AlertDialog chooseGoodsDialog = new PosDialog.PosBuilder(context, PosDialog.CONFIRM_TYPE_WITH_TEXT_SHADOW)
                .setRealDialogContent(R.layout.dialog_choose_goods_new)
                .setDialogTitle("一码多物")
                .setSingleConfirmText("添加")
                .setWidthAndHeight(360, 420)
                .show();

        final LRecyclerView chooseGoodsRv = chooseGoodsDialog.getView(R.id.root_dialog_choose_goods);
        final ADA_DialogChooseReturnGoods adapter = new ADA_DialogChooseReturnGoods(context);
        initRecyclerView(context, chooseGoodsRv, adapter, null);
        adapter.update(newList, true);

        adapter.setOnCheckedChangedListener(new ADA_DialogChooseReturnGoods.OnCheckedChangedListener() {
            @Override
            public void onCheckChanged() {
                TextView goodsCount = chooseGoodsDialog.getView(R.id.tv_bottom_text);
                StringSpanUtils str = StringSpanUtils.init().addStr("总共选择 " + adapter.getSelectedCount() +" 件商品");
                str.setForeColorWithKeyAndPosition(StringUtils.get(adapter.getSelectedCount()),
                        0, context.getResources().getColor(R.color.black_color));
                goodsCount.setText(str.build());
            }

        });

        chooseGoodsDialog.setOnClickListener(R.id.lb_confirm, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                List<ReturnGoodsEntity> selectedList = adapter.getSelectedList();
                if (ListUtil.isListEmpty(selectedList)) {
                    ToastUtil.makeText(context, "请选择商品", false);
                    return;
                }

                // 把选择的商品添加到 cartList，然后同步购物车信息
                for (ReturnGoodsEntity goodsBean : selectedList) {
                    if (!AddReturnCartUtil.isGoodEnoughToReturn(localList, goodsBean)) {
                        PosToast.INSTANCE.makeShortToast(context, "退货数量不得大于购买数量！");
                        return;
                    }
                }

                if (listener != null) {
                    listener.OnSelectReturnGoodsList(selectedList);
                }

                chooseGoodsDialog.dismiss();

            }
        });

    }

    public static void finish(ACT_BillReturn activity) {
        activity.finish();
        activity.overridePendingTransition(R.anim.right_in, R.anim.right_out);
    }

}
